﻿
using System.Text;
using Protocol.SouthGridProtocol.Blocks;
using Protocol.SouthGridProtocol.Decoder;
using Protocol.SouthGridProtocol.Encoder;
using PacoToolKit.Format;
namespace Protocol.SouthGridProtocol.Model
{
    public class C0FH
    {
        private static string afn = "0F";
        private static string head = "68";
        private static string bottom = "16";

        private static Seq seq = new Seq(0, 1, 1, 1, 6);
        private static C c = new C(0, 1, 1, null, 0, 11);//prm=1,funcode=11

        /// <summary>
        /// 生产文件传输启动帧
        /// </summary>
        /// <param name="concentrator"></param>
        /// <param name="fileTransferStartBlock">文件传输启动帧FD1数据</param>
        /// <returns></returns>
        public static string encodeFileTransferStart(string concentrator, FileTransferStartBlock fileTransferStartBlock)
        {
            //主站启动的发送帧的MSA应为非零值,终端启动发送帧的MSA应为零，其主站响应帧的MSA也应为零
            concentrator = concentrator.Substring(0, 12) + "01";
            StringBuilder sb = new StringBuilder();
            sb.Append("0000" + Formater.ReverseHexString("E3010001") + fileTransferStartBlock.toString());
            //StringBuilder msgsb = new StringBuilder();
            string length = BaseEncoder.CalcLength(c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString());
            string cs = BaseEncoder.CalcCSCode(c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString());
            return head + length + length + head + c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString() + cs + bottom;
        }

        /// <summary>
        /// 文件传输、文件启动传输返回的状态码
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string DecodeFileTransferReturnCode(string msg)
        {
            string dataRemain = msg.Remove(0, 32);
            string da = dataRemain.Substring(0, 4);
            string di = dataRemain.Substring(4, 8);
            string err = dataRemain.Substring(12, 2);
            return err;
        }

        /// <summary>
        /// 获取di
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static string GetFileTransferDi(string msg)
        {
            string dataRemain = msg.Remove(0, 32);
            string da = dataRemain.Substring(0, 4);
            string di = dataRemain.Substring(4, 8);
            return Formater.ReverseHexString(di);
        }

        /// <summary>
        /// 解析读取文件的属性
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="startIndex"></param>
        /// <param name="endIndex"></param>
        /// <returns></returns>
        public static FileTransferCheckBlock DecodeFileCheck(string msg,int startIndex,int endIndex)
        {
            string dataRemain = msg.Remove(0, 32);
            string da = dataRemain.Substring(0, 4);
            string di = dataRemain.Substring(4, 8);
            dataRemain = dataRemain.Remove(0, 12);
            string hex_fileType = dataRemain.Substring(0, 4);
            string hex_fileGroupCount = dataRemain.Substring(4, 4);
            string hex_fileSize = dataRemain.Substring(8, 8);
            string ASCII_fileName = dataRemain.Substring(16, 64);
            //2个字节总校验
            string hex_startIndex = dataRemain.Substring(84, 4);
            string hex_endIndex = dataRemain.Substring(88, 4);
            string hex_errorcount = dataRemain.Substring(92, 4);

            string fileGroupCount = Formater.HEXtoDEC(Formater.ReverseHexString(hex_fileGroupCount));
            string fileSize = Formater.HEXtoDEC(Formater.ReverseHexString(hex_fileSize));
            string fileName = Formater.ASCLLHexBytesToString(Formater.HexStringToBytesKeep(ASCII_fileName));
            string startIndexstr = Formater.HEXtoDEC(Formater.ReverseHexString(hex_startIndex));
            string endIndexstr = Formater.HEXtoDEC(Formater.ReverseHexString(hex_endIndex));
            string errorCount = Formater.HEXtoDEC(Formater.ReverseHexString(hex_errorcount));
            return new FileTransferCheckBlock(hex_fileType, int.Parse(fileGroupCount), int.Parse(fileSize), fileName, int.Parse(startIndexstr), int.Parse(endIndexstr), int.Parse(errorCount));
        }

        /// <summary>
        /// 文件传输内容帧
        /// </summary>
        /// <param name="concentrator"></param>
        /// <param name="fileTransferBlock">文件传输内容帧FD2数据</param>
        /// <returns></returns>
        public static string encodeFileTransfer(string concentrator, FileTransferBlock fileTransferBlock)
        {
            //主站启动的发送帧的MSA应为非零值,终端启动发送帧的MSA应为零，其主站响应帧的MSA也应为零
            concentrator = concentrator.Substring(0, 12) + "01";
            StringBuilder sb = new StringBuilder();
            sb.Append("0000" + Formater.ReverseHexString("E3010002") + fileTransferBlock.toString());

            string length = BaseEncoder.CalcLength(c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString());
            string cs = BaseEncoder.CalcCSCode(c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString());
            return head + length + length + head + c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString() + cs + bottom;
        }

        /// <summary>
        /// 查询文件信息
        /// </summary>
        /// <param name="concentrator"></param>
        /// <param name="fileTransferCheckBlock"></param>
        /// <returns></returns>
        public static string encodeFileCheck(string concentrator, FileTransferCheckBlock fileTransferCheckBlock)
        {
            //主站启动的发送帧的MSA应为非零值,终端启动发送帧的MSA应为零，其主站响应帧的MSA也应为零
            concentrator = concentrator.Substring(0, 12) + "01";
            StringBuilder sb = new StringBuilder();
            sb.Append("0000" + Formater.ReverseHexString("E3010003") + fileTransferCheckBlock.toString());

            string length = BaseEncoder.CalcLength(c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString());
            string cs = BaseEncoder.CalcCSCode(c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString());
            return head + length + length + head + c.encodeC() + concentrator + afn + seq.encodeSeqString() + sb.ToString() + cs + bottom;
        }
    }
}
